package top.jle.wx.proxy.facade.impl;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import top.jle.jpa.basic.util.ExceptionUtils;
import top.jle.wx.proxy.constant.InvoiceOption;
import top.jle.wx.proxy.domain.invoice.CardInfo;
import top.jle.wx.proxy.domain.invoice.Invoice;
import top.jle.wx.proxy.exception.ExceptionCode;
import top.jle.wx.proxy.facade.InvoiceFacade;
import top.jle.wx.proxy.feign.ResultCheckUtil;
import top.jle.wx.proxy.feign.invoice.InvoiceAPIFeign;
import top.jle.wx.proxy.util.GlobalThreadLocalUtil;
import top.jle.wx.proxy.util.RandomStringUtil;

@Component("invoiceFacade")
public class InvoiceFacadeImpl implements InvoiceFacade {

    @Autowired
    private InvoiceAPIFeign invoiceAPIFeign;

    @Override
    public String createCard(CardInfo cardInfo){
        if(cardInfo ==null || !cardInfo.valid()){
            ExceptionUtils.throwInvalidParamException();
        }
        JSONObject json = new JSONObject();
        json.put("invoice_info",cardInfo);
        JSONObject result = invoiceAPIFeign.createCard(json);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.CREATE_CARD_FAIL);
        }
        return result.getString("card_id");
    }

    @Override
    public String uploadPDF(MultipartFile pdf){
        if(pdf == null){
            ExceptionUtils.throwInvalidParamException();
        }
        JSONObject result = invoiceAPIFeign.uploadPDF(pdf);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.UPLOAD_PDF_FAIL);
        }
        return result.getString("s_media_id");
    }

    @Override
    public String getPDF(String mediaId){
        if(StringUtils.isBlank(mediaId)){
            ExceptionUtils.throwInvalidParamException();
        }
        JSONObject request = new JSONObject();
        request.put("action","get_url");
        request.put("s_media_id",mediaId);
        JSONObject result = invoiceAPIFeign.getPDF(request);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.GET_PDF_FAIL);
        }
        return result.getString("pdf_url");
    }

    @Override
    public InvoiceOption.InsertInvoiceResponse insertInvoice(String orderNo, String cardId, Invoice invoice){
        if(StringUtils.isBlank(orderNo) || StringUtils.isBlank(cardId) || invoice==null){
            ExceptionUtils.throwInvalidParamException();
        }
        JSONObject request = new JSONObject();
        request.put("order_id",orderNo);
        request.put("card_id",cardId);
        request.put("appid", GlobalThreadLocalUtil.getAppId());

        JSONObject cardExt = new JSONObject();
        cardExt.put("nonce_str", RandomStringUtil.randomLettersAndDigits(8));

        JSONObject userCard = new JSONObject();
        userCard.put("invoice_user_data",invoice);

        cardExt.put("user_card",userCard);

        request.put("card_ext",cardExt);

        JSONObject result = invoiceAPIFeign.insert(request);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.INSERT_INVOICE_FAIL);
        }
        return request.toJavaObject(InvoiceOption.InsertInvoiceResponse.class);
    }

}
